package com.hanxing.kxcharge.module.operations.controller.admin.equipment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.CommonResult;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;
import static com.hanxing.kxcharge.framework.common.pojo.CommonResult.success;

import com.hanxing.kxcharge.framework.excel.core.util.ExcelUtils;

import com.hanxing.kxcharge.framework.operatelog.core.annotations.OperateLog;
import static com.hanxing.kxcharge.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hanxing.kxcharge.module.operations.controller.admin.equipment.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.equipment.EquipmentDO;
import com.hanxing.kxcharge.module.operations.service.equipment.EquipmentService;

@Tag(name = "管理后台 - 电桩管理")
@RestController
@RequestMapping("/operations/equipment")
@Validated
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    @PostMapping("/create")
    @Operation(summary = "创建电桩管理")
    @PreAuthorize("@ss.hasPermission('operations:equipment:create')")
    public CommonResult<Long> createEquipment(@Valid @RequestBody EquipmentSaveReqVO createReqVO) {
        return success(equipmentService.createEquipment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新电桩管理")
    @PreAuthorize("@ss.hasPermission('operations:equipment:update')")
    public CommonResult<Boolean> updateEquipment(@Valid @RequestBody EquipmentSaveReqVO updateReqVO) {
        equipmentService.updateEquipment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除电桩管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('operations:equipment:delete')")
    public CommonResult<Boolean> deleteEquipment(@RequestParam("id") Long id) {
        equipmentService.deleteEquipment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得电桩管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('operations:equipment:query')")
    public CommonResult<EquipmentRespVO> getEquipment(@RequestParam("id") Long id) {
        EquipmentDO equipment = equipmentService.getEquipment(id);
        return success(BeanUtils.toBean(equipment, EquipmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得电桩管理分页")
    @PreAuthorize("@ss.hasPermission('operations:equipment:query')")
    public CommonResult<PageResult<EquipmentRespVO>> getEquipmentPage(@Valid EquipmentPageReqVO pageReqVO) {
        PageResult<EquipmentDO> pageResult = equipmentService.getEquipmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出电桩管理 Excel")
    @PreAuthorize("@ss.hasPermission('operations:equipment:export')")
    @OperateLog(type = EXPORT)
    public void exportEquipmentExcel(@Valid EquipmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentDO> list = equipmentService.getEquipmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "电桩管理.xls", "数据", EquipmentRespVO.class,
                        BeanUtils.toBean(list, EquipmentRespVO.class));
    }

}